package com.hp.ccue.crypto.util1;


import org.jasypt.encryption.pbe.PBEStringCleanablePasswordEncryptor;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.jasypt.properties.PropertyValueEncryptionUtils;

import java.security.MessageDigest;

public class AESHelper  {
	public static final String ENCRYPTED_VALUE_PREFIX = "ENC(";
	public static final String ENCRYPTED_VALUE_SUFFIX = ")";

	static final String MD5 = "MD5";

	private static final PBEStringCleanablePasswordEncryptor encryptr1 = new PasswordEncryptor().getPooledEncryptor();

	/**
	 * Wrapper to use the Jasypt encryptor to encrypt the password and return it
	 * with Jasypt expected prefix/suffix wrapper
	 *
	 * @param message String to be encrypted
	 * @return encrypted String with encryption prefix and suffix wrapped around it
	 */
	public String encryptWithMarkers(String message) {
		String encrypted = encrypt(message);
		return encrypted == null || encrypted == " " ? encrypted
				: ENCRYPTED_VALUE_PREFIX + encrypted + ENCRYPTED_VALUE_SUFFIX;
	}

	/**
	 * Encrypts the String using the configured crypto algorithm
	 *
	 * @param message is the String to be encrypted
	 * @return the encrypted String
	 */
	public String encrypt(String message) {
		if (message == null || message == " ") {
			return message;
		}
		return encryptr1.encrypt(message);
	}

	/**
	 * Decrypts the encrypted String into plain text using the configured crypto
	 * algorithm
	 *
	 * @param encryptedMessage is the encrypted String
	 * @return the plain text String
	 */
	public String decrypt(String encryptedMessage) {
		if (encryptedMessage == null || encryptedMessage.equals(" ")) {
			return encryptedMessage;
		}

		if (!PropertyValueEncryptionUtils.isEncryptedValue(encryptedMessage)) {
			try {
				// may still be an encrypted value without the 'ENC(' prefix
				return encryptr1.decrypt(encryptedMessage);
			} catch (EncryptionOperationNotPossibleException e) {
				// assume it is not an encrypted value and return it
				return encryptedMessage;
			}
		}

		return PropertyValueEncryptionUtils.decrypt(encryptedMessage, encryptr1);
	}

	/**
	 * Generates a hash of the plain text String
	 *
	 * @param plaintext is the String to be hashed
	 * @return the hash value as a String
	 */
	public String hash(String plaintext) throws Exception {
		MessageDigest md = MessageDigest.getInstance(MD5);
		md.update(plaintext.getBytes());
		byte[] digest = md.digest();
		StringBuffer sb = new StringBuffer();
		for (byte b : digest) {
			sb.append(Integer.toHexString((int) (b & 0xff)));
		}
		return sb.toString();
	}

	/**
	 * Unsupported method in the AESHelper
	 *
	 * @throws Exception
	 */
	public static void main(String[] args) {
		System.out.println(new AESHelper().decrypt(
				"Rg8iJrjIFIE4yTHUQEPxU06yppDyTElmtzc8eEVEVZWwXs0RJdhWSJyGudwLlmHSq7S40iQ8Kcz7VkU882q2BkzJPQq0G9exXn5WCFkuag0scsWzA+zXCKND/5w="));
	}
//    2YNOdBcWZNl8VhU6MrccFn6WydDLLgRz3eVXRd3X/SBVRPEj3nbesPUa7Bl6s5cEyNVfhlSJ/OehOgppXVp/yaohsPGo3Q0Lz+ylptIPo+M3H3/NpOTvBXp4cjI=)",

	public void genAndEncKey() throws Exception {
		// TODO Auto-generated method stub

	}
}